/**
 * 通用删除
 */
$('.ajax-delete').on('click', function() {
	var _href = $(this).attr('href');
	layer.open({
		shade: 0.1,
		content: '确定删除？',
		btn: ['确定', '取消'],
		yes: function(index) {
			$.ajax({
				url: _href,
				type: "post",
				success: function(info) {
					if(info.code === 1) {
						setTimeout(function() {
							location.href = info.url;
						}, 800);
					}
					layer.msg(info.msg);
				}
			});
			layer.close(index);
		}
	});
	return false;
});

/**
 * url参数获取
 * @param  name 参数名
 */
function getUrlParam(pname) {
	var name, value;
	var str = location.href; //取得整个地址栏
	var num = str.indexOf("?")
	str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
	var arr = str.split("&"); //各个参数放到数组里
	for(var i = 0; i < arr.length; i++) {
		num = arr[i].indexOf("=");
		if(num > 0) {
			name = arr[i].substring(0, num);
			value = arr[i].substr(num + 1);
			if(pname == name) {
				return value;
			}
		}
	}
}

//获取用户当前地址
function getAddress(){
	
	var script2 = document.createElement("script");
	script2.type = "text/javascript";
	script2.src = "https://developer.baidu.com/map/jsdemo/demo/convertor.js";
	document.body.appendChild(script2);
	
	
	var script1 = document.createElement("script");
	script1.type = "text/javascript";
	script1.src = "https://api.map.baidu.com/api?v=2.0&ak=grTHl4v1H6uGFLChfz0aB0uQG4XWQv0k&callback=my_getAddress";
	document.body.appendChild(script1);
	
}
/**
 * 单图片上传
 * #upload 上传控件名
 * viewUrl: true 显示预览图路径
 */
function upload_one(viewUrl,upload,upname) {
	//初始化fileinput控件（第一次初始化）
	if(upload==""||upload==undefined){
		upload="upload";
	}
	if(upname==""||upname==undefined){
		upname="thumb";
	}
	$("#"+upload).fileinput({
		language: 'zh', //设置语言
		uploadUrl: __UPLOAD_URL__, //上传的地址
		allowedFileExtensions: ['jpg', 'png', 'gif'], //接收的文件后缀
		showUpload: false, //是否显示上传按钮
		showCaption: true, //是否显示标题
		showRemove: false, //是否显示移除
		dropZoneEnabled: false, //是否显示拖曳区域
		showUploadedThumbs: false, //是否保已上传图片缓存图
		layoutTemplates: { actions: '' },
		initialPreview: [
			"<img src='" + viewUrl + "' class='file-preview-image' style='height:200px;width:300px;'>",
		],
	}).on("filebatchselected", function(event, files) {
		alert(1);
		$(this).fileinput("upload");
	}).on("fileuploaded", function(event, data) {
		alert(2);
		if(data.response.code == 1) {
			$("#"+upname).val(data.response.src);
		} else {
			layer.msg(data.response.msg);
		}
	}).on('filecleared', function() {
		alert(3);
		$("#"+upname).val('');
	});
}

/*
 * 表单ajax提交
 * @remark 表单ID必须为myForm，action不填默认为当前页面
 */
function ajaxForm() {
	$('#myForm').ajaxSubmit({
		beforeSubmit: function() {
			$(":button").prop("disabled", true);
		}, // 此方法主要是提交前执行的方法，根据需要设置
		success: function(info) {
			$(":button").prop("disabled", false);
			if(info.code == 1) {
				layer.msg(info.msg, {
					icon: 1,
					time: 800,
					shade: 0.1
				}, function(index) {
					layer.close(index);
					window.location.href = info.url;
				});
			}else if(info.code==200||info.code==406){
				layer.msg(info.msg, {
					icon: 1,
					time: 800,
					shade: 0.1
				}, function(index) {
					layer.close(index);
					$("#tx_"+info.id,window.parent.document).remove();
					window.parent.call_back();
				});
			} else {
				layer.msg(info.msg, {
					icon: 0,
					time: 1500,
					shade: 0.1
				}, function(index) {
					layer.close(index);
				});
				return false;
			}
		}, // 这是提交后的方法
		dataType: 'json'
	});
}

function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}

function setCookie(name,value)
{
var exp = new Date();
exp.setTime(exp.getTime() + 3*60*60*24*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg))
	return unescape(arr[2]);
	else
	return null;
}
